package com.gw.sample.einkinputdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.guowen.lib.einkupdatehelper.EinkUpdateHelper;
import com.guowen.lib.einkupdatehelper.EinkUpdateMode;


public class MainActivity extends Activity implements View.OnClickListener {

    EditText editText;
    View rootView;

    EditText et_stratyge;
    EditText et_mode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rootView = findViewById(R.id.rootview);

        editText = findViewById(R.id.edittext);
        Button btn_set_strategy = findViewById(R.id.btn_set_strategy);
        Button btn_set_mode = findViewById(R.id.btn_set_mode);
        Button btn_set_defaultmode = findViewById(R.id.btn_set_defaultmode);
        Button btn_set_back = findViewById(R.id.btn_set_back);

        btn_set_defaultmode.setVisibility(View.VISIBLE);
        btn_set_back.setVisibility(View.VISIBLE);

        et_stratyge = findViewById(R.id.et_stratyge);
        et_mode = findViewById(R.id.et_mode);

        btn_set_strategy.setOnClickListener(this);
        btn_set_mode.setOnClickListener(this);
        btn_set_defaultmode.setOnClickListener(this);
        btn_set_back.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * 隐藏软件盘方法的其中一种
     *
     * @param token
     */
    protected void hideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * EditText获取焦点并显示软键盘
     */
    public static void showSoftInputFromWindow(EditText editText) {
        editText.requestFocus();
        InputMethodManager inputManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_set_strategy:

                int strategy = Integer.valueOf(et_stratyge.getText().toString());

                EinkUpdateHelper.setDefaultUpdateStrategy(strategy,
                        EinkUpdateHelper.getEinkUpdateMode(),
                        EinkUpdateHelper.getEinkUpdateMiddleMode(),
                        EinkUpdateHelper.getEInkUpdateEndMode());


                Log.d("mode", "new DefaultUpdateStrategy:" + EinkUpdateHelper.getDefaultUpdateStrategy());
                Log.d("mode", "new DefaultUpdatemode-start:" + EinkUpdateHelper.getEinkUpdateMode());
                Log.d("mode", "new DefaultUpdatemode-middle:" + EinkUpdateHelper.getEinkUpdateMiddleMode());
                Log.d("mode", "new DefaultUpdatemode-end:" + EinkUpdateHelper.getEInkUpdateEndMode());
                break;

            case R.id.btn_set_mode:

                int mode = Integer.valueOf(et_mode.getText().toString());

                EinkUpdateHelper.setEinkUpdateMode(mode);

                Log.d("mode", "new DefaultUpdateStrategy:" + EinkUpdateHelper.getDefaultUpdateStrategy());
                Log.d("mode", "new DefaultUpdatemode-start:" + EinkUpdateHelper.getEinkUpdateMode());
                Log.d("mode", "new DefaultUpdatemode-middle:" + EinkUpdateHelper.getEinkUpdateMiddleMode());
                Log.d("mode", "new DefaultUpdatemode-end:" + EinkUpdateHelper.getEInkUpdateEndMode());
                break;

            case R.id.btn_set_defaultmode:
                EinkUpdateHelper.setDefaultUpdateStrategy(
                        EinkUpdateMode.EINK_DISPLAY_STRATEGY_ALL_FLIP_WITHOUT_LAST,
                        EinkUpdateMode.EINK_DISPLAY_MODE_GC16_LOCAL,
                        EinkUpdateMode.EINK_DISPLAY_MODE_GC16_FULL,
                        EinkUpdateMode.EINK_DISPLAY_MODE_GC16_LOCAL);

                Log.d("mode", "new DefaultUpdateStrategy:" + EinkUpdateHelper.getDefaultUpdateStrategy());
                Log.d("mode", "new DefaultUpdatemode-start:" + EinkUpdateHelper.getEinkUpdateMode());
                Log.d("mode", "new DefaultUpdatemode-middle:" + EinkUpdateHelper.getEinkUpdateMiddleMode());
                Log.d("mode", "new DefaultUpdatemode-end:" + EinkUpdateHelper.getEInkUpdateEndMode());
                break;

            case R.id.btn_set_back:
                EinkUpdateHelper.setDefaultUpdateStrategy(
                        EinkUpdateMode.EINK_DISPLAY_STRATEGY_ALL_FLIP_WITHOUT_LAST,
                        EinkUpdateMode.EINK_DISPLAY_MODE_GC16_FULL,
                        EinkUpdateMode.EINK_DISPLAY_MODE_GC16_FULL,
                        EinkUpdateMode.EINK_DISPLAY_MODE_GC16_FULL);

                Log.d("mode", "new DefaultUpdateStrategy:" + EinkUpdateHelper.getDefaultUpdateStrategy());
                Log.d("mode", "new DefaultUpdatemode-start:" + EinkUpdateHelper.getEinkUpdateMode());
                Log.d("mode", "new DefaultUpdatemode-middle:" + EinkUpdateHelper.getEinkUpdateMiddleMode());
                Log.d("mode", "new DefaultUpdatemode-end:" + EinkUpdateHelper.getEInkUpdateEndMode());
                break;
        }
    }

    enum EinkMode {
        EINK_DISPLAY_MODE_INIT(1, 1),
        EINK_DISPLAY_MODE_DU_FULL(2, 2),
        EINK_DISPLAY_MODE_GC16_FULL(3, 4),
        EINK_DISPLAY_MODE_A2(4, 16),
        EINK_DISPLAY_MODE_GC16_LOCAL(5, 132),
        EINK_DISPLAY_MODE_GL16(6, 32),
        EINK_DISPLAY_MODE_DU_RECT(7, 1026),
        EINK_DISPLAY_MODE_GC16_RECT(8, 1028),
        EINK_DISPLAY_MODE_A2_RECT(9, 1040),
        EINK_DISPLAY_MODE_GC16_LOCAL_RECT(10, 1156),
        EINK_DISPLAY_MODE_GL16_RECT(11, 1056);

        int index;
        int value;

        EinkMode(int index, int value){
            this.index = index;
            this.value = value;
        }
    }
}
